package com.manning.unlockingandroid;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class ReviewCriteria extends Activity //1
{
 private static final int MENU_GET_REIEWS = Menu.FIRST;
 private EditText location; //2
 private Spinner cuisine; //2
 private Button getReviews; //2
 @Override
 public void onCreate(final Bundle savedInstanceState) //3
 {
  super.onCreate(savedInstanceState);
  this.set.ContentView(R.layout.review_criteria); //4
  this.location =(EditText) this.findViewById(R.id.location); //5
  this.cuisine = (Spinner) this.findViewById(R.id.cuisine); //5
  this.getReviews = (Button) this.findViewById(R.id.get_reviews_button); //5
  ArrayAdapter<String> cuisines = new ArrayAdapter<String>(this, 
		  R.layout.spinner_view,this.getRecources().getStringArray(R.array.cuisines)); //6
  cuisines.setDropDownViewResource(R.layout.spinner_view_dropdown); //7
  this.cuisine.setAdapter(cuisines); //8
  getReviews.setClickListener(new OnClickListener(){ //9
	  public void onClick(View v)
	  { ReviewCriteria.this.handleGetReviews();
	  }
      });
 }
 
 @Override
 public boolean onCreateOptionsMenu(final Menu) //1
 { 
  super.onCreateOPtionsMenu(menu);
  menu.add(0, ReviewCriteria.Menu_GET_REVIEWS, 0, R.string.menu_get_reviews).setIcon(android.R.drawable.ic_menu_more);
  return true;
 }
 
 @Override
 public boolean onMenuItemsSelected(final int featureId final MenuItem item)
 {//2
	 switch(item.getItemId()){
	 case MENU_GET_REVIEWS:
		 this.handleGetReviews();
		 return true;
 }
return super.onMenuItemSelected(featureId, item);
}

private void handleGetReview() //3
{
 if(!this.validate())
 {
	 return;
 }
}

RestaurrantFinderApplication application = (RestaurrantFinderApplication) this.getApplication() //4
application.setReviewCriteriaLocation(this.location.getText().toString());
Intent intent = new intent(Constants.INTENT_ACTION_VIEW_LIST); //5
this.startActivity(intent); //6
}

private boolean validate()
{
 boolean valid = true ;
 StringBuffer validationText = new StringBuffer();
 if((this.location.getText() == null) || this.location.getText().toString().equals(""))
 {
  validationText.append(this.getResources().getString(R.string.location_not_supplied_message));
  valid = false;
 }
 
if (!valid)
{
 new AlertDialog.Builder(this).setTitle(this.getResources().getString(R.string.alert_lable)).setMessage(validationText.toString()).setPositiveButton("Continue", new android.content.DialogInternface.OnClickListener(){ //8
	 public void onCLick(final DialogInterface dialog, final int arg1){}
 }).show();
 validationText = null ;
} //end of if
return valid;
}

}
